import { BasicColumn, FormSchema } from '@apps/antd-admin/src/components/Table';
import { render } from '@apps/antd-admin/src/hooks/render/renderUtils';
import {Api, shopCateGoryAllList, shopCateGoryList} from './api';
import {isExistByUsername} from "@apps/antd-admin/src/views/sys/user/data/user.api";
import { rules } from '@pkg/utils';
export const columns: BasicColumn[] = [
  {
    title: '商家图片',
    dataIndex: 'shopImg',
    width: 100,
    customRender: render.renderImage,
  },
  {
    title: '商家名称',
    dataIndex: 'shopName',
    width: 180,
    customRender: render.renderTxt,
  },
  {
    title: '商家类别',
    dataIndex: 'shopTypeName',
    width: 130,
  },
  {
    title: '上架',
    dataIndex: 'isShelf',
    width: 100,
    align: 'center',
    sorter: true,
    rowKey: 'shopId',
    renderApi: Api.setIsShelf,
    customRender: render.renderSwitch,
  },
  {
    title: '置顶',
    dataIndex: 'isTop',
    width: 100,
    align: 'center',
    sorter: true,
    rowKey: 'shopId',
    renderApi: Api.setIsTop,
    customRender: render.renderSwitch,
  },
  {
    title: '推荐',
    dataIndex: 'recommend',
    width: 100,
    align: 'center',
    rowKey: 'shopId',
    sorter: true,
    renderApi: Api.setRecommend,
    customRender: render.renderSwitch,
  },
  {
    title: '营业时间',
    dataIndex: 'shopTime',
    width: 130,
  },
  {
    title: '门店电话',
    dataIndex: 'shopPhone',
    width: 130,
  },
  {
    title: '商家地址',
    dataIndex: 'shopAddress',
    customRender: render.renderTxt,
    width: 140,
  },
  {
    title: '人均消费',
    sorter: true,
    dataIndex: 'consumeMoney',
    width: 100,
  },
  {
    title: '商家账户',
    dataIndex: 'shopAccount',
    width: 100,
  },
  {
    title: '创建日期',
    dataIndex: 'createTime',
    width: 100,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'shopName',
    label: '商家名称',
    component: 'Input',
    colProps: { span: 5 },
  },
  {
    field: 'shopType',
    label: '商家分类',
    component: 'ApiCascader',
    colProps: { span: 5 },
    componentProps: ({ formModel }) => {
       return {
         api: shopCateGoryList,
         apiParamKey: 'typeId',
         labelField: 'typeName',
         asyncFetchParamKey:'parentId',
         valueField: 'typeId',
         initFetchParams: {
           parentId: '0',
         },
         isLeaf: (record) => {
           return (record.parentId != '0')
         },
         onChange(values) {
           formModel.shopTypes=values.join(",");
         }
       }
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'shopId',
    label: '商家Id',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '商家分类',
    field: 'shopGroup',
    component: 'ApiSelect',
    required: true,
    componentProps: ({formModel, formActionType}) => {
      return {
        api: Api.listByParentId,
        labelField: 'typeName',
        valueField: 'typeId',
        immediate: true,
        showSearch: true,
        onChange(typeId) {
          formModel.seriesId = null;
          const {updateSchema} = formActionType;
          if(typeId) {
            updateSchema(
              {
                field: 'shopType',
                componentProps: {
                  api: Api.listByParentId,
                  immediate: true,
                  labelField: 'typeName',
                  valueField: 'typeId',
                  showSearch: true,
                  params: {
                    parentId: typeId
                  }
                },
              }
            ).then();
          }
        },
      };
    }
  },
  {
    label: '二级分类',
    field: 'shopType',
    component: 'ApiSelect',
    ifShow: ({ values }) => {
      return !!values.shopGroup;
    },
    required: true,
  },
  {
    field: 'airportId',
    label: '机场Id',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '商家名称',
    field: 'shopName',
    component: 'Input',
    required: true,
  },
  {
    label: '商家地址',
    field: 'shopAddress',
    component: 'InputTextArea',
    required: true,
  },
  {
    label: '营业时间',
    field: 'shopTime',
    component: 'Input',
  },
  {
    label: '门店电话',
    field: 'shopPhone',
    component: 'Input',
  },

  /*{
    label: '商家后台账户',
    field: 'shopAccount',
    component: 'InputTextArea',
  },*/
  {
    label: '店铺位置',
    field: 'shopPosition',
    component: 'ApiSelect',
    componentProps: {
      api: Api.listByDictType,
      labelField: 'dicDataTitle',
      valueField: 'dicDataValue',
    },
  },
  {
    label: '商业筛选',
    field: 'shopSearchTag',
    component: 'ApiSelect',
    componentProps: {
      api: Api.shopSearchTag,
      labelField: 'dicDataTitle',
      valueField: 'dicDataValue',
    },
  },

  {
    label: '经营范围',
    field: 'businessScope',
    component: 'InputTextArea',
  },
  {
    label: '人均消费',
    field: 'consumeMoney',
    component: 'Input',
    suffix: '元',
    itemProps:{
      extra:"填写后可在详情页展示人均消费(非必填)",
    },
  },
  {
    label: '详情标题',
    field: 'shopDescTitle',
    defaultValue: "店铺简介",
    componentProps:{
      defaultValue: "店铺简介",
    },
    itemProps:{
      extra:"示例词：如店铺简介、订房须知等",
    },
    component: 'Input',
  },
  {
    label: '商家详情',
    field: 'shopDesc',
    component: 'TyEditor',
    componentProps:{
      height:400
    },
  },
  {
    label: '排序',
    field: 'shopSort',
    component: 'InputNumber',
  },
  {
    label: '商家封面图',
    field: 'shopImg',
    component: 'ImageUpload',

    itemProps:{
      extra:"建议尺寸200*200",
    },
    required: true
  },
  {
    label: '商家相册',
    field: 'shopImgs',
    component: 'ImageUpload',
    itemProps:{
      extra:"建议尺寸750*420",
    },
    required: true,
    componentProps: {
      fileMax: 5,
    },
  },

 /* {
    label: '商家详情',
    field: 'shopContent',
    component: 'TyEditor',
    required: true
  },*/
];

export const shopAccountFormSchema: FormSchema[] = [{
    label: '账户名',
    field: 'userName',
    required: true,
    component: 'Input',
    dynamicDisabled: ({ values }) => {
      if (values.userId) {
        return true;
      } else {
        return false;
      }
    },
    dynamicRules: ({ model }) => {
      return [
        {
          required: true,
          validator: async (_, value) => {
            if (model.userId) {
              return Promise.resolve();
            }
            if (!value) {
              return Promise.reject('请输入账号名');
            }
            let isExist = await isExistByUsername(value);
            if (isExist) {
              return Promise.reject('该账号名已存在');
            }
            return Promise.resolve();
          },
          trigger: 'change',
        },
      ];
    },
  },
  {
    label: '姓名',
    field: 'nickName',
    component: 'Input',
    required: true
  },
  {
    label: '手机号',
    field: 'mobile',
    component: 'Input',
    required: true,
    itemProps:{
      extra:"手机号做为初始密码",
    },
  },
  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
  },
]
